package com.wl.lib.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.view.isVisible
import com.wl.lib.base.databinding.ViewCommonStateBinding
import com.wl.lib.mvi.LoadUiState
import com.wl.lib.utils.ErrorStateHelper
import com.wl.lib.utils.ResUtils


// Created by wangliang on 2024/4/28.
class PageStateView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    companion object {
        private const val TAG = "PageStateView"
    }

    private val binding = ViewCommonStateBinding.inflate(LayoutInflater.from(context), this, true)

    private var state: State = State.LOADING
    private var reloadListener: (() -> Unit)? = null

    init {
        binding.errorView.errorRoot.setOnClickListener {
            reloadListener?.invoke()
        }
        updateState(state)
    }

    private fun updateState(state: State) {
        this.state = state
        when (state) {
            State.LOADING -> binding.stateRoot.displayedChild = 0
            State.EMPTY -> binding.stateRoot.displayedChild = 1
            State.ERROR -> {
                binding.stateRoot.displayedChild = 2
                ErrorStateHelper.updateErrorTextAndImage(
                    binding.errorView.errorIcon,
                    binding.emptyView.emptyTextView
                )
            }
        }

        binding.stateRoot.bringToFront()
    }

    fun emptyIconAndText(iconResId: Int, emptyText: String?) {
        if (iconResId == 0) {
            binding.emptyView.emptyIcon.isVisible = false
        } else {
            binding.emptyView.emptyIcon.isVisible = true
            binding.emptyView.emptyIcon.setImageDrawable(ResUtils.getDrawable(iconResId))
            binding.emptyView.emptyTextView.text = emptyText
        }
    }

    fun setReloadListener(reloadListener: () -> Unit) {
        this.reloadListener = reloadListener
    }

    fun render(state: LoadUiState, firstLoadFinished: Boolean, showMainPage: (() -> Unit)) {
        Log.d(TAG, "loadUiState: $state")
        when (state) {
            is LoadUiState.INIT -> {
                updateState(State.LOADING)
                isVisible = true
            }

            is LoadUiState.Error -> {
                Log.e(TAG, state.msg)
                if (!firstLoadFinished) {
                    updateState(State.ERROR)
                    isVisible = true
                }
            }

            is LoadUiState.ShowMainView -> {
                showMainPage.invoke()
                isVisible = false
            }

            is LoadUiState.Loading -> {
                if (state.show) {
                    updateState(State.LOADING)
                    isVisible = true
                }
                // 隐藏 Loading 这里无需处理，由于处在此种环境，无论哪种状态，都会改变 PageStateView 状态
            }

        }
    }

    enum class State {
        LOADING, EMPTY, ERROR
    }
}